package com.zaoshui.hudada.model.vo;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.zaoshui.hudada.model.dto.question.QuestionContentDTO;
import com.zaoshui.hudada.model.entity.Question;
import com.zaoshui.hudada.model.entity.User;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 应用视图
 */
@Data
public class QuestionVO implements Serializable {

    /**
     * id
     */
    private Long id;

    /**
     * 题目内容（json格式）
     */
    private List<QuestionContentDTO> questionContent;

    /**
     * 应用 id
     */
    private Long appId;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    private UserVO user;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /**
     * 包装类转对象
     *
     * @param appVo
     * @return
     */
    public static Question voToObj(QuestionVO appVo) {
        if (appVo == null) {
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(appVo, question);
        question.setQuestionContent(JSONUtil.toJsonStr(appVo.getQuestionContent()));
        return question;
    }

    /**
     * 对象转包装类
     *
     * @param question
     * @return
     */
    public static QuestionVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionVO questionVo = new QuestionVO();
        BeanUtils.copyProperties(question, questionVo);
        questionVo.setQuestionContent(JSONUtil.toList(question.getQuestionContent(), QuestionContentDTO.class));
        return questionVo;
    }
}
